<?php
declare (strict_types=1);

// +----------------------------------------------------------------------
// | SCApi
// +----------------------------------------------------------------------
// | 版权所有 2021~2022 贾二小 [ http://www.ipsoul.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://www.ipsoul.com
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// | 免费声明 ( http://doc.ipsoul.com/disclaimer )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/jiaerxiao/scapi
// | github 代码仓库：https://github.com/jiaerxiao/scapi
// +----------------------------------------------------------------------

namespace app\system\service;

use app\common\service\BaseService;
use app\common\service\ToolService;
use app\system\facade\ValidateRuleFacade;
use think\App;

class ValidateService extends BaseService
{
    /**
     * 初始化
     * @param App $app
     */
    public function __construct(App $app)
    {
        parent::__construct($app, '参数验证', 'validate');
    }

    /**
     * 根据请求获取验证规则
     * @param string $app
     * @param string $con
     * @param string $act
     * @return array
     * @author 贾二小
     * @since 2021/12/8
     */
    public function getData(string $app, string $con, string $act): array
    {
        $rule = $message = [];
        try {//获取验证规则
            $map = [];
            array_push($map, ['app', '=', $app], ['con', '=', $con], ['act', '=', $act]);
            $validateData = $this->info($map);
            if (empty($validateData)) {
                return [];
            }//获取验证规则明细
            $map = [];
            array_push($map, ['parentId', '=', $validateData['id']]);
            $validateRuleList = ValidateRuleFacade::list($map);//数据组装
            array_map(function ($item) use (&$rule, &$message) {
                if ($item['value'] == null) {
                    $rule[$item['paramName']][] = $item['key'];
                } else {
                    $rule[$item['paramName']][] = $item['key'] . ':' . $item['value'];
                }
                $message[$item['paramName'] . '.' . $item['key']] = $item['message'];
            }, $validateRuleList->toArray());
        } catch (\Exception $e) {
            ToolService::error($e->getMessage());
        }
        return [$rule, $message];
    }

    /**
     * 表格数据
     * @author 贾二小
     * @since 2021/11/20
     */
    public function tableList(array $paramData): array
    {
        $map = [];
        return $this->table($map);
    }
}